package com.yhyang.example.ribbonclienthystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2018.10.09
 */
@RestController
public class RibbonHystrixController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hystrix")
    @HystrixCommand(fallbackMethod = "hostErrorFallback")
    public ResponseEntity<String> host() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://SERVICE-PROVIDER/hystrix", String.class);
        return responseEntity;
    }

    public ResponseEntity<String> hostErrorFallback() {
        System.out.println(Thread.currentThread().getName());
        return ResponseEntity.ok("error");
    }
}
